package javaUnit;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.RandomStringUtils;

public class randomType {
	
	public static void main(String[] args) {
		for(int i=0;i<100;i++) {
			System.out.println(getRandomString(3,"!@#$%^ &"));
		}
	}

	/**
	 * 根据类型返回随机值目前仅支持int，float，double，varchar，char,date
	 * 
	 * @param type SQL的类型
	 * @return
	 */
	public static Object reValue(String type) {
		String temp=type.toLowerCase();
		if(temp.contains("varchar"))temp="varchar";
		switch (temp) {
		case "int":
			return (int) (Math.random() * 101);
		case "float":
		case "double":
			return Math.random() * 101;
		case "varchar":
			return "'" + RandomStringUtils.randomAscii(5).replace("'", "").replace("\\", "")+ "'";
		case "char":
			return "'" + RandomStringUtils.randomAscii(1) + "'";
		case "date":
			return "'" + getDateByDay(5000) + "'";
		default:
			return null;
		}
	}

	/**
	 * 
	 * @param length 字符串的长度
	 * @return 指定长度的汉字
	 */

	public static String getChineseString(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			sb.append((char)(0x4e00+(int)(Math.random()*(0x9fa5-0x4e00+1))));
		}
		return sb.toString();
	}
	
	/**
	 * 生成数字字母组成的指定长度的字符串
	 * 
	 * @param length 字符串的长度
	 * @return 返回指定长度的字符串
	 */
	public static String getRandomString(int length) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(str.length());
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 指定字符集合内生成指定长度字符串
	 * 
	 * @param length 字符串的长度
	 * @return 返回指定长度的字符串
	 */
	public static String getRandomString(int length, String str) {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(str.length());
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 生成随机日期字符串
	 * 
	 * @return 当前时间减去随机数[0,1)*5000的天数
	 */
	public static String getDateByDay(int number) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, (int) (Math.random() * -number));
		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/**
	 * 生成随机int类型的数据
	 * @param x
	 * @param y
	 * @param type
	 * @return YaNb:[x,y) 左闭右开 /NaYb:(x,y] 左开右闭 /YaYb:[x,y] 左闭右闭 /NaNb:(x,y) 左开右开
	 */
	public static int getInt(int x, int y, String type) {
		switch (type) {
		case "YaNb": {
			return x + (int) (Math.random() * (y - x));
		}
		case "NaYb": {
			return y - (int) (Math.random() * (y - x));
		}
		case "YaYb": {
			return x + (int) (Math.random() * (y - x + 1));
		}
		case "NaNb": {
			while(true) {
				int temp=(int)(Math.random() * (y - x));
				if(temp!=0) 
					return x + temp;
				}
		}
		default:
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * 生成随机double类型的数据
	 * @param x
	 * @param y
	 * @param type
	 * @return YaNb:[x,y) 左闭右开 /NaYb:(x,y] 左开右闭 /YaYb:[x,y] 左闭右闭 /NaNb:(x,y) 左开右开
	 */
	public static double getDouble(int x, int y, String type) {
		switch (type) {
		case "YaNb": {
			return x + Math.random() * (y - x);
		}
		case "NaYb": {
			return y - Math.random() * (y - x);
		}
		case "YaYb": {
			return x + Math.random() * (y - x + 1);
		}
		case "NaNb": {
			while(true) {
				double temp=Math.random() * (y - x);
				if(temp!=0.0) 
					return x + temp;
				}
		}
		default:
			return  Double.NaN;
		}
	}

	/**
	 * @return 随机的UUID
	 */
	public static UUID getUUID() {;
		return UUID.randomUUID();
	}
}
